package player.view.actions;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.KeyStroke;
import javax.swing.filechooser.FileNameExtensionFilter;

import player.model.Track;
import player.util.TrackLoader;
import player.view.MainWindow;

public class OpenFolderAction extends AbstractAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4943043951075819258L;

	private MainWindow owner;
	
	public OpenFolderAction(MainWindow owner){
		this.setOwner(owner);
		putValue(NAME, "Open Folder");
		putValue(SHORT_DESCRIPTION, "Dodavanje direktorijuma u plejlistu");
		putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_D, KeyEvent.CTRL_MASK));
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		JFileChooser fc = new JFileChooser();
		fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		fc.setAcceptAllFileFilterUsed(false);
		fc.setApproveButtonText("Otvori");
		fc.setDialogTitle("Otvaranje foldera");
		fc.setFileFilter(new FileNameExtensionFilter("MP3 fajlovi", "mp3"));
		int returnVal = fc.showOpenDialog(owner);
		
		if(returnVal == JFileChooser.APPROVE_OPTION){
			
				File dir = fc.getSelectedFile();
				File array[] = dir.listFiles();
				System.out.println(array.length);
				for( File f: array ){
					try {
						Track t = TrackLoader.loadTrack(f);
						owner.getPlayer().addToPlaylist(t);
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			
			
			
		}
	}

	public void setOwner(MainWindow owner) {
		this.owner = owner;
	}

	public MainWindow getOwner() {
		return owner;
	}

}
